import os
import time

from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow, QAction, QMessageBox, QFileDialog, QProgressDialog, QInputDialog, QLineEdit
from PyQt5.uic import loadUi
from util.SSHUtil import SSHUtil


class MainWindow:
    def __init__(self):
        self.mainWindow = QMainWindow()
        loadUi("ui/main.ui", self.mainWindow)
        self.mainWindow.setWindowIcon(QIcon('controller/icon.jpg'))
        toolbar_1_action = QAction('远程关机', self.mainWindow.toolBar)
        toolbar_2_action = QAction('远程重启', self.mainWindow.toolBar)
        toolbar_3_action = QAction('本地关机', self.mainWindow.toolBar)
        toolbar_4_action = QAction('发送文件', self.mainWindow.toolBar)
        toolbar_5_action = QAction('接收文件', self.mainWindow.toolBar)
        toolbar_6_action = QAction('屏幕锁屏', self.mainWindow.toolBar)
        toolbar_7_action = QAction('电子签到', self.mainWindow.toolBar)
        toolbar_8_action = QAction('设备状态', self.mainWindow.toolBar)
        toolbar_9_action = QAction('退出', self.mainWindow.toolBar)

        toolbar_1_action.triggered.connect(lambda: self.toolbar_action(1))
        toolbar_2_action.triggered.connect(lambda: self.toolbar_action(2))
        toolbar_3_action.triggered.connect(lambda: self.toolbar_action(3))
        toolbar_4_action.triggered.connect(lambda: self.toolbar_action(4))
        toolbar_5_action.triggered.connect(lambda: self.toolbar_action(5))
        toolbar_6_action.triggered.connect(lambda: self.toolbar_action(6))
        toolbar_7_action.triggered.connect(lambda: self.toolbar_action(7))
        toolbar_8_action.triggered.connect(lambda: self.toolbar_action(8))
        toolbar_9_action.triggered.connect(lambda: self.toolbar_action(9))
        self.mainWindow.toolBar.addAction(toolbar_1_action)
        self.mainWindow.toolBar.addAction(toolbar_2_action)
        self.mainWindow.toolBar.addAction(toolbar_3_action)
        self.mainWindow.toolBar.addAction(toolbar_4_action)
        self.mainWindow.toolBar.addAction(toolbar_5_action)
        self.mainWindow.toolBar.addAction(toolbar_6_action)
        self.mainWindow.toolBar.addAction(toolbar_7_action)
        self.mainWindow.toolBar.addAction(toolbar_8_action)
        self.mainWindow.toolBar.addAction(toolbar_9_action)

    def show(self):
        self.mainWindow.show()

    def toolbar_action(self, index):

        if index == 1:
            # 远程关机
            reply = QMessageBox.question(self.mainWindow, '信息', ' 确认关机？')
            if reply == QMessageBox.Yes:
                # WinRmUtil.getClient().run_cmd('shutdown /p')
                SSHUtil.getSSHClient().exec_command('shutdown /p')
        elif index == 2:
            #  远程重启
            reply = QMessageBox.question(self.mainWindow, '信息', ' 确认重启？')
            if reply == QMessageBox.Yes:
                # WinRmUtil.getClient().run_cmd('shutdown -r -t 0')
                SSHUtil.getSSHClient().exec_command('shutdown - r - t 0')
        elif index == 3:
            # 本地关机
            reply = QMessageBox.question(self.mainWindow, '信息', ' 确认关机？')
            if reply == QMessageBox.Yes:
                os.system('shutdown /s /t 0')
        elif index == 4:
            # 发送文件
            file, _ = QFileDialog.getOpenFileName(self.mainWindow, "选择发送文件", "./", "All Files (*)")
            if file:
                sftp_client = SSHUtil.getSSHClient().open_sftp()
                sftp_client.put(file, file.split('/')[-1])
                sftp_client.close()
                QMessageBox.information(self.mainWindow, '提示', ' 上传成功！')
        elif index == 5:
            # 接受文件
            text, okPressed = QInputDialog.getText(self.mainWindow, "选择文件", "请输入文件名", QLineEdit.Normal)
            if okPressed and text != '':
                sftp_client = SSHUtil.getSSHClient().open_sftp()
                sftp_client.get(text, text)
                sftp_client.close()
                QMessageBox.information(self.mainWindow, '提示', '%s下载完成' % text)

        elif index == 6:
            reply = QMessageBox.question(self.mainWindow, '信息', ' 确认锁屏？')
            if reply == QMessageBox.Yes:
                # WinRmUtil.getClient().run_cmd('rundll32.exe user32.dll LockWorkStation')
                SSHUtil.getSSHClient().exec_command('rundll32.exe user32.dll LockWorkStation')
        elif index == 7:
            # 确认签到
            reply = QMessageBox.question(self.mainWindow, '信息', ' 确认签到？')
            if reply == QMessageBox.Yes:
                self.mainWindow.label_2.setText('签到：已签到')
        elif index == 8:
            # progress = QProgressDialog(self.mainWindow)
            # progress.setWindowTitle("请稍等")
            # progress.setLabelText("正在操作...")
            # progress.setCancelButtonText("取消")
            # progress.setRange(0, 100)
            # progress.setValue(10)
            # progress.show()
            stdin, stdout, stderr = SSHUtil.getSSHClient().exec_command('wmic OS get FreePhysicalMemory')
            str = stdout.read().decode().replace('FreePhysicalMemory', '可用内存：')
            # progress.setValue(25)

            stdin, stdout, stderr = SSHUtil.getSSHClient().exec_command('wmic ComputerSystem get TotalPhysicalMemory')
            str = str + stdout.read().decode().replace('TotalPhysicalMemory', '总内存：')
            # progress.setValue(50)

            stdin, stdout, stderr = SSHUtil.getSSHClient().exec_command('wmic cpu get loadpercentage')
            str = str + stdout.read().decode().replace('LoadPercentage', 'CPU使用率：') + '%'
            # stdin, stdout, stderr = SSHUtil.getSSHClient().exec_command('systeminfo')
            # str = str + stdout.read().decode()

            # wmic memorychip
            # progress.setValue(75)

            self.mainWindow.content.setText(str)


        elif index == 9:
            self.mainWindow.close()

# 127.0.0.1
# xiaohao
# 0310
